home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-12-12 | 2.7 KB | 142 lines | [TEXT/KAHL] |
- /***
- *
- * Debug.cp - debug routines
- *
- * Original code: Copyright (c) 1991, by David Michael Betz. All rights reserved
- * Modifications and additions: Copyright © by Christopher E. Hyde, 1995
- *
- ***/
-
- #include "Bob.h"
-
-
- // instruction output formats
- enum { kFmtNone, kFmtByte, kFmtWord, kFmtLit };
-
- struct TOpDef {
- char fName[5];
- uchar fOpCode:6;
- uchar fFormat:2;
- };
-
- static TOpDef pOpTab[] = {
- #define Op(n, f) { #n, op##n, kFmt##f },
- Op(BRT, Word)
- Op(BRF, Word)
- Op(BR, Word)
- Op(LIT, Lit)
- Op(REF, Lit)
- Op(SET, Lit)
- Op(AREF, Byte)
- Op(ASET, Byte)
- Op(TREF, Byte)
- Op(TSET, Byte)
- Op(MREF, Byte)
- Op(MSET, Byte)
- Op(VREF, None)
- Op(VSET, None)
- Op(CALL, Byte)
- Op(RTS, None)
- Op(SEND, Byte)
- Op(TSPC, Byte)
- Op(NIL, None)
- Op(PUSH, None)
- Op(NOT, None)
- Op(NEG, None)
- Op(ADD, None)
- Op(SUB, None)
- Op(MUL, None)
- Op(DIV, None)
- Op(REM, None)
- Op(SHL, None)
- Op(SHR, None)
- Op(BAND, None)
- Op(BOR, None)
- Op(XOR, None)
- Op(BNOT, None)
- Op(LT, None)
- Op(LE, None)
- Op(EQ, None)
- Op(NE, None)
- Op(GE, None)
- Op(GT, None)
- Op(INC, None)
- Op(DEC, None)
- Op(DUP2, None)
- Op(NEW, None)
- Op(INT, Word)
- {0, 0, 0}
- };
-
-
- static void
- NewLine (void)
- {
- gOutput.NewLine();
- }
-
-
- // Dump the instructions in a code object to stderr
- void
- DumpProcedure (ConstValue code)
- {
- const TVector* codeV = code->fVec;
- int len = SLen(&codeV->fData[kIByteCodes]);
-
- for (int lc = 0; lc < len; )
- lc += DumpInstruction(codeV, lc);
- NewLine();
- }
-
-
- // Dump a single bytecode instruction to stderr
- int
- DumpInstruction (const TVector* code, int lc)
- {
- ConstValue vec = code->fData;
- if (lc == 0) { // Show the function name
- NewLine();
- if (vec[kIClass].fType == tClass) { // Show the class name
- TId name;
- GetCString(name, sizeof(name), clgetname(&vec[kIClass]));
- PrintErrF("%s::", name);
- }
- Print(&stderr_iostream, false, &vec[kIName]);
- NewLine();
- }
-
- // Get bytecode pointer for this instruction
- CodePtr cp = (CodePtr) vec[kIByteCodes].fStr->fData + lc;
-
- // Show the address and opcode
- uchar opCode = *cp++;
- PrintErrF("%04X: %02X ", lc, opCode);
-
- // Display the operands
- for (TOpDef* op = pOpTab; op->fName[0]; ++op)
- if (opCode == op->fOpCode) {
- switch (op->fFormat) {
- case kFmtNone:
- PrintErrF(" \t%s\r", op->fName);
- return 1;
- case kFmtByte:
- PrintErrF("%02X \t%s %02X\r", *cp, op->fName, *cp);
- return 2;
- case kFmtWord:
- PrintErrF("%02X %02X \t%s %02X%02X\r",
- *cp, cp[1], op->fName, *cp, cp[1]);
- return 3;
- // case kFmtLit:
- default:
- PrintErrF("%02X \t%s %02X ; ", *cp, op->fName, *cp);
- Print(&stderr_iostream, true, &vec[*cp]);
- NewLine();
- return 2;
- }
- }
-
- // Unknown opcode
- PrintErrF(" <UNKNOWN>\r");
- return 1;
- }
-